<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <span>电商管理后台系统</span>
      </div>
      <el-button type="warning" plain @click="clickQuit">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="this.isCollapse ? '64px' : '200px'">
        <!-- 折叠 -->
        <div class="collapase" @click="clickCollapse">||||</div>
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#313743"
          text-color="#fff"
          active-text-color="#3398db"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activepath"
        >
          <!-- 一级菜单区域 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in asideMenuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单区域 -->
            <el-menu-item
              :index="'/' + item.path"
              v-for="item in item.children"
              :key="item.id"
              @click="saveActivePath('/' + item.path)"
            >
              <i class="el-icon-menu"></i>
              <span>{{ item.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主要内容区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// 引入图标库
import '@/assets/font/iconfont.css'
import { asideMenu } from '@/request/api.js'
export default {
  name: 'Home',
  data() {
    return {
      // 侧边栏菜单是否折叠
      isCollapse: false,
      asideMenuList: [],
      // 一级图标
      iconObj: {
        125: 'iconfont icon-yonghuguanli',
        103: 'iconfont icon-quanxianguanli',
        101: 'iconfont icon-shangpinguanli',
        102: 'iconfont icon-dingdanguanli',
        145: 'iconfont icon-shujutongji',
      },
      // 激活的path
      activepath: '',
    }
  },
  methods: {
    // 保存激活的菜单,页面加载就读出来
    saveActivePath(ActivePath) {
      sessionStorage.setItem('activemenupath', ActivePath)
      this.activepath = ActivePath
    },
    // 点击折叠侧边栏菜单
    clickCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 点击退出
    clickQuit() {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    },

    // 侧边栏菜单信息
    async getMenus() {
      // const { data } = await this.$http.get('menus')
      const { data: res } = await asideMenu()
      // console.log(res.data)
      this.asideMenuList = res.data
    },
  },
  mounted() {
    this.getMenus()
    this.activepath = sessionStorage.getItem('activemenupath')
  },
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
/* 头部区域 */
.el-header {
  background-color: #363d40;
  display: flex;
  align-items: center;
  justify-content: space-between;
  span {
    font-size: 20px;
    color: white;
  }
}
/* 侧边栏区域 */
.el-aside {
  background-color: #313743;
  // 折叠
  .collapase {
    color: white;
    letter-spacing: 2px;
    text-align: center;
    cursor: pointer;
    line-height: 24px;
  }
}
/* 内容区域 */
.el-main {
  background-color: #e9edf1;
}
.el-menu {
  border-right: none;
}

.iconfont {
  margin-right: 10px;
}
</style>
